<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<html>
	<head>
		<base href="<%=basePath%>">
		<title>WebSocket 测试</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="sgs-frog,sgs">
		<meta http-equiv="description" content="自在飞花轻似梦,无边丝雨细如愁.">
	</head>
	<body>
		<button onclick="javascript:connect();">连接</button>
		<button onclick="javascript:disconnect();">断开</button>
		<br/>
		send:
		<button onclick="javascript:send();">发送消息</button>
		<textarea id="ss" rows="15" cols="25">hello server!</textarea>
		receive:
		<textarea id="rr" rows="15" cols="55"></textarea>
		<script type="text/javascript">
			var url = "ws://127.0.0.1:14151/";
			var ws;
			var connect = function(){
				if(ws){
					alert("已经连接");
					return;
				}
				ws = new WebSocket(url);
				ws.onmessage = function(event){
					alert(event);
				}
				ws.onclose = function(){
					alert("close");
				}
				ws.onopen = function(){
					alert("open");
				}
			}
			var send = function(){
				var ss = document.getElementById("ss");
				var value = ss.value;
				if(value.length>0){
					receive("send："+value);
					if(!ws){
						alert("未连接");
						return;
					}
					alert(ws.send)
				}
				ss.value = "";
			}
			var receive = function(msg){
				var rr = document.getElementById("rr");
				rr.value = rr.value+msg+"\n";
			}
			connect();
		</script>
	</body>
</html>
